import org.openkinect.*;
import org.openkinect.processing.*;

// Showing how we can farm all the kinect stuff out to a separate class
KinectTracker tracker;
// Kinect Library object
Kinect kinect;

KinectControlMove kcm;
int angleCamera;
String txt;

void setup() {
  size(640,480);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
  kcm = new KinectControlMove(this);
  txt = new String("");
  angleCamera = 20;
  kinect.tilt(angleCamera);
}

void draw() {
  background(255);
  frame.setLocation(screen.width-650,screen.height-540);
  // Run the tracking analysis
  tracker.track();
  // Show the image
  tracker.display();  // Let's draw the "lerped" location
  PVector locationRight = tracker.getPosRight();
  fill(100,250,50,200);
  noStroke();
  ellipse(locationRight.x,locationRight.y,20,20);
  
  PVector locationLeft = tracker.getPosLeft();
  fill(204, 153, 0,100);
  noStroke();
  ellipse(locationLeft.x,locationLeft.y,20,20);
  
  String s;
  s = kcm.start(locationRight,locationLeft,tracker.getTracked());
  if(!s.equals("None"));
    txt = s;
    //kcm.secondHand(locationLeft); 
  // Display some info
  int t = tracker.getThreshold();
  fill(0);  
  //text("Dernière detection :"+txt,10,500);
      //text("Vector : "+locationLeft.x+","+locationRight.x,10,500);
  //text(txt,10,500);
  
   
}

void keyPressed() {
  int t = tracker.getThreshold();
  if (key == CODED) {
    if (keyCode == UP) {
      t+=5;
      tracker.setThreshold(t);
    } 
    else if (keyCode == DOWN) {
      t-=5;
      tracker.setThreshold(t);
    } 
  }
  else if(key == 'Z')
    up();
  else if(key == 'S')
    down();
  }

void up(){
  angleCamera++;
  kinect.tilt(angleCamera);
}

void down(){
  angleCamera--;
  kinect.tilt(angleCamera);
}


void stop() {
  tracker.quit();
  super.stop();
}

